<template>
    <div class="login-main">
        <div class="card">
            <div class="logo">
                <img src="~/static/picture/logo.png" alt="">
            </div>
            <div class="title">登錄您的管理用戶</div>
            <el-form ref="form" :model="form" label-width="0">
                <el-form-item label="登錄用戶">
                    <el-input v-model="form.username" placeholder="請輸入用戶名"></el-input>
                </el-form-item>
                <el-form-item label="登錄密碼">
                    <el-input type="password" v-model="form.password" placeholder="請輸入密碼"></el-input>
                </el-form-item>
                <el-form-item style="text-align: center;">
                    <el-button :loading="loading" style="width: 200px;" type="primary" @click="onSubmit">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<style scoped lang="less">
.el-button--primary {
    background-color: #201D1E;
    border-color: #201D1E;
}

.el-button--primary:focus,
.el-button--primary:hover {
    background-color: #201D1E;
    border-color: #201D1E;
}

.login-main {
    background-color: #f1f4fd;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;

    .card {
        width: 420px;
        height: auto;
        background-color: #FFFFFF;
        padding: 50px;
        border-radius: 10px;
        box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);

        .title {
            margin: 20px 0;
            font-size: 18px;
            text-align: center;
            letter-spacing: 2px;
            color: rgb(117, 127, 149);
        }

        .logo {
            width: 300px;
            margin: 0 auto;

            img {
                width: 100%;
            }
        }
    }

    /deep/ .el-form-item__label {
        width: 100% !important;
        text-align: left;
    }

    /deep/ .el-form-item {
        margin-bottom: 30px;
    }
}
</style>

<script>
import { Message } from 'element-ui';
import axios from "~/plugins/axios.js";

export default {
    name: 'adminIndexPage',
    ssr: false,
    head() {
        return {
            style: [
                {
                    type: 'text/css',
                    cssText: `
                        html, body {
                            margin: 0;
                            padding: 0;
                            height: 100%;
                        }

                        #__layout {
                            height: 100%;
                        }

                        #__nuxt {
                            height: 100%;
                        }
                    `
                }
            ]
        };
    },
    data() {
        return {
            form: {
                username: '',
                password: ''
            },
            loading: false
        }
    },
    methods: {
        async onSubmit() {
            if (!this.form.username) {
                Message.warning('請輸入用戶名');
                return false;
            }
            if (!this.form.password) {
                Message.warning('請輸入登錄密碼');
                return false;
            }
            this.loading = true;
            const res = await axios.post('/admin/system.Login/login', this.form).finally(() => {
                this.loading = false;
            });
            if (res.code == 200) {
                localStorage.setItem("user_id", res.data.user_id);
                localStorage.setItem("nickname", res.data.nickname);
                localStorage.setItem("username", res.data.username);
                localStorage.setItem("AdminToken", res.data.AdminToken);
                this.$router.push('/admin/board');
            } else {
                Message.error(res.msg);
            }
        }
    },
    mounted() {
    }
}
</script>
